package com.loongxun.playwithyou.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Order;
import com.loongxun.playwithyou.ui.activity.playuser.BillInfoActivity;
import com.loongxun.playwithyou.ui.activity.playuser.OrderInfoActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/7/20.
 */
public class OrderAdapter extends BaseAdapter {

    private Context context;
    public List<Order> list;
    private LayoutInflater inflater;
    private int type = 0;
    private String statisId;

    public void setList(List<Order> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setStatisId(String statisId) {
        this.statisId = statisId;
    }

    public OrderAdapter(Context context, List<Order> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    public OrderAdapter(Context context, List<Order> list,int type) {
        this.context = context;
        this.list = list;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_order_member, null);

            holder.ll_item_order_list = (LinearLayout) convertView.findViewById(R.id.ll_item_order_list);
            holder.item_order_list_orderno = (TextView) convertView.findViewById(R.id.item_order_list_orderno);
            holder.item_order_list_orderdate = (TextView) convertView.findViewById(R.id.item_order_list_orderdate);
            holder.item_order_list_hoursnum = (TextView) convertView.findViewById(R.id.item_order_list_hoursnum);
            holder.item_order_list_orderstate = (TextView) convertView.findViewById(R.id.item_order_list_orderstate);
            if (type == 1) {
                holder.item_order_list_orderstate.setVisibility(View.GONE);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Order item = list.get(position);
        holder.item_order_list_orderno.setText("订  单  号："+item.getOrder_no());
        holder.item_order_list_orderdate.setText("预约时间："+item.getPlay_start_time());
        holder.item_order_list_hoursnum.setText("预约时长：共计" +item.getHours_num()+"小时");

        if (type != 1) {
            if (item.getOrder_state() == 3) {
                holder.item_order_list_orderstate.setText("已完成");
            } else {
                holder.item_order_list_orderstate.setText("未完成");
            }
        }

        holder.ll_item_order_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type == 1) {
                    Intent intent = new Intent(context, BillInfoActivity.class);
                    intent.putExtra("orderNo", item.getOrder_no());
                    intent.putExtra("statisId", statisId);
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, OrderInfoActivity.class);
                    intent.putExtra("orderNo", item.getOrder_no());
                    context.startActivity(intent);
                }
            }
        });

        return convertView;
    }

    class ViewHolder {
        private TextView item_order_list_orderno,item_order_list_orderdate,
                item_order_list_hoursnum,item_order_list_orderstate;
        private LinearLayout ll_item_order_list;
    }

}
